package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder;
import com.czl.base.data.bean.tengyun.StorehouseBean;
import com.czl.module_base.utils.GlideUtils;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.databinding.ItemStockWarnBinding;

import java.util.List;

/**
 * @author by xubin
 * PS: Not easy to write code, please indicate.
 */
public class StockWarnAdapter extends BaseQuickAdapter<StorehouseBean.StockWarnBean, BaseDataBindingHolder<ItemStockWarnBinding>> {

    public StockWarnAdapter(int layoutResId, @Nullable List<StorehouseBean.StockWarnBean> data) {
        super(layoutResId, data);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(@NonNull BaseDataBindingHolder<ItemStockWarnBinding> bindingHolder, StorehouseBean.StockWarnBean item) {
        ItemStockWarnBinding dataBinding = bindingHolder.getDataBinding();
        if (dataBinding == null) {
            return;
        }
        GlideUtils.loadImage(getContext(), dataBinding.ivGoods, R.mipmap.pic_default_loading, item.getSortImg());
        dataBinding.tvGoodsName.setText(item.getSortName());
        dataBinding.tvGoodsCode.setText("型号：" + (item.getSortModel() == null ? "" : item.getSortModel()));
        dataBinding.tvGoodsNorm.setText("下限：" + (item.getWarnNum() == null ? 0 : item.getWarnNum()));
        dataBinding.tvStock.setText("库存：" + (item.getStockNum() == null ? 0 : item.getStockNum()));
        dataBinding.tvAlertNum.setText("警告 " + (getAlertNum(item)));
    }

    private int getAlertNum(StorehouseBean.StockWarnBean bean) {
        if (bean.getWarnNum() == null) {
            return 0;
        }
        if (bean.getStockNum() == null) {
            return bean.getWarnNum();
        }
        return bean.getWarnNum() - bean.getStockNum();
    }
}
